package com.tdam2013.grupo07.conectivity;

import java.util.Iterator;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;

import com.tdam2013.grupo07.conectivity.ConectividadListener.State;
import com.tdam2013.grupo07.data.ConectividadDBHandler;
import com.tdam2013.grupo07.data.DatabaseHelperSingleton;
import com.tdam2013.grupo07.util.Constantes;

public class ConectividadBroadcastRecieverGlobal extends BroadcastReceiver 
{
	@Override
	public void onReceive(Context context, Intent intent) 
	{
		ConectividadListener cListener=ConectividadListenerSingleton.getInstance().getConectividadListener();
		String action = intent.getAction();
		if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) 
		{
			boolean noConnectivity = intent.getBooleanExtra(ConnectivityManager.EXTRA_NO_CONNECTIVITY, false);
			ConectividadDBHandler target=new ConectividadDBHandler(DatabaseHelperSingleton.getInstance(context).getDatabaseHelper());
			Message message = Message.obtain(target);
			Bundle data=new Bundle();
			data.putBoolean(Constantes.EXTRA_NO_CONNECTIVITY, noConnectivity);
			if(!noConnectivity)
			{
				ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
				NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
				data.putInt(Constantes.EXTRA_CONNECTIVITY_TYPE, networkInfo.getType());
			}
			message.setData(data);
			target.sendMessage(message);
		}
	}
};